<template>
  <div class="container">
    <normal-title
      :title="title"
      :back="true"
    ></normal-title>
    <div class="header">
      <img
        class="bg"
        src="../../images/bg.png"
        mode="widthFix"
      >
      <div class="row">
        <div>{{categoryInfo!==null?categoryInfo.label:''}}</div>
        <!-- <div class="icon_1">|</div>
        <div>FRENCH PRODUCING AREA</div> -->
      </div>
      <p class="text">{{categoryInfo!==null?categoryInfo.description : ''}}</p>
      <div class="group-btn">
        <div
          class="btn"
          v-for="(item,index) in ['综合','价格','评价','年份']"
          :key=index
          @click="choiceSort(index)"
        >
          <div
            :class="activedItem==index?'btnSelected':''"
            style="height:100rpx;line-height:100rpx"
          >{{item}}</div>
          <div
            v-show="index!=0"
            class="icon_2"
          >
            <div :class="!rankItem&&activedItem==index?'iconSelected':''">▲</div>
            <div :class="rankItem&&activedItem==index?'iconSelected':''">▼</div>
          </div>
        </div>
      </div>
    </div>

    <ul class="list">
      <li
        v-for="(item, index) in products"
        :key=index
      >
        <a
          hover-class="none"
          :href="'/pages/detail/main?id='+item.id"
          class="content"
        >
          <img
            class="img"
            :src="item.image"
            mode="aspectFit"
          >
          <div class="title">{{item.product_name}}</div>
          <div class="buy">
            <!-- <span class="icon">￥</span> -->
            <!-- <span class="price">{{item.real_price}}</span> -->
            {{item.label?item.label:''}}
            <span class="oldPrice">原价{{item.original_price}}</span>
          </div>
        </a>
      </li>
    </ul>
    <bottom-btn />
  </div>
</template>

<script>
import NormalTitle from "@/components/NormalTitle";
import BottomBtn from "@/components/BottomBtn";
import WinesList from "@/components/WinesList";
import { request } from "@/utils";
export default {
  components: {
    NormalTitle,
    WinesList,
    BottomBtn
  },
  data() {
    return {
      title: "",
      activedItem: 0,
      rankItem: false,
      page: 1,
      hasMore: true,
      products: [],
      categoryInfo: null
    };
  },
  methods: {
    choiceSort(index) {
      if (this.activedItem === index) {
        this.rankItem = !this.rankItem;
      } else {
        this.activedItem = index;
        this.rankItem = true;
      }
      this.getProducts(true);
    },
    async getProducts(init) {
      if (init) {
        this.page = 1;
        this.hasMore = true;
      }
      var data = {
        page: this.page,
        perPage: 12
      };
      var sortName = ["-sort", "real_price", "score", "product_name"];
      data.sort = sortName[0];
      if (this.activedItem > 0) {
        data.sort = sortName[this.activedItem];
        if (!this.rankItem) {
          data.sort = "-" + sortName[this.activedItem];
        }
      }
      var result;
      if (this.$root.$mp.query.keyword) {
        data.keyword = this.$root.$mp.query.keyword;
        result = await request("/v1/product/search", "get", data);
      } else if (this.$root.$mp.query.product_id) {
        data.product_id = this.$root.$mp.query.product_id;
        data.perPage = 1000;
        result = await request("/v1/product/search", "get", data);
      } else if (
        this.$root.$mp.query.cateId ||
        this.$root.$mp.query.parent_id
      ) {
        data.category_id = this.$root.$mp.query.cateId || 0;
        data.parent_id = this.$root.$mp.query.parent_id || 0;
        result = await request("/v1/product", "get", data);
        this.categoryInfo = result.data.category[0] || null;
      }
      if (result.data) {
        if (init) {
          this.products = result.data.items;
        } else {
          this.products = this.products.concat(result.data.items);
        }
        if (result.data.items.length === 0) {
          this.hasMore = false;
        }
      }
    },
    async getSortData() {
      getProducts(true);
    }
  },
  mounted() {
    (this.activedItem = 0), (this.rankItem = false), (this.categoryInfo = null);
    this.products = [];
    this.title = this.$root.$mp.query.title;
    this.keyword = this.$root.$mp.query.keyword;
    this.getProducts(true);
  },
  async onReachBottom() {
    if (!this.hasMore) {
      return;
    }
    this.page += 1;
    this.getProducts();
  }
};
</script>

<style scoped lang="scss">
.container {
  .header {
    position: relative;
    color: #fff;
    font-size: 30rpx;
    margin-bottom: 30rpx;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    height: 340rpx;
    .bg {
      box-sizing: border-box;
      width: 750rpx;
      position: absolute;
      z-index: -1;
      top: 0rpx;
      left: 0rpx;
    }
    .row {
      height: 60rpx;
      width: 630rpx;
      overflow: hidden;
    }
    .text {
      width: 630rpx;
      font-size: 20rpx;
      line-height: 38rpx;
      height: 150rpx;
      overflow: hidden;
    }
    .group-btn {
      font-size: 28rpx;
      margin: 0 25rpx;
      display: flex;
      align-items: center;
      justify-content: space-around;
      width: 700rpx;
      height: 100rpx;
      background: #fff;
      color: #000;
      border-radius: 15rpx;
      box-shadow: rgb(200, 200, 200) 0 0 10rpx;
      .btnSelected {
        color: #cfb685;
        border-bottom: 4rpx solid #cfb685;
      }
      .iconSelected {
        color: #cfb685;
      }
      .btn {
        display: flex;
        justify-content: space-around;
        align-items: center;
        height: 100%;
        .icon_2 {
          font-size: 15rpx;
          margin-left: 8rpx;
          div {
            line-height: 15rpx;
          }
        }
      }
    }
  }
  .list {
    display: flex;
    margin: 0 25rpx;
    justify-content: space-between;
    flex-wrap: wrap;
    li {
      .content {
        width: 330rpx;
        // height: 450rpx;
        display: flex;
        flex-direction: column;
        // font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
        justify-content: flex-start;
        margin-bottom: 20px;
        .img {
          width: 330rpx;
          height: 320rpx;
        }
        .title {
          font-size: 14px;
          margin-top: 15rpx;
          width: 330rpx;
          line-height: 20px;
          font-weight: bold;
          color: #343434;
          overflow: hidden;
          text-overflow: ellipsis;
          display: -webkit-box;
          -webkit-line-clamp: 2;
          -webkit-box-orient: vertical;
        }
        .buy {
          white-space: nowrap;
          color: #b93a39;
          margin-top: 10px;
          // border: 1rpx solid #000;
          font-weight: bold;
          font-size: 16px;
          line-height: 15px;
          overflow: hidden;
          width: 330rpx;
          text-overflow: ellipsis;
          .icon {
            font-size: 12px;
            font-weight: bold;
          }
          .oldPrice {
            text-decoration: line-through;
            font-size: 10px;
            color: #6d6d6d;
            display: inline-block;
            margin-left: 5rpx;
          }
        }
      }
    }
  }
}
</style>
